; This function is for collecting key input depending on whether a player
; is selecting menu objects, moving or firing.

; The type of context will determine what keys are required

Icon_Menu .equ 1
Standard_In_Game .equ 2
Return .equ 1
EndLoop .equ 2

Second .equ 0
Mode .equ 1
Del .equ 2
Alpha .equ 3
X_T_Theta_N .equ 4
Stat .equ 5
Math .equ 6
Apps .equ 7
Program .equ 8


#macro DrawA(y, x)

 push hl
 push de
 push bc
 push af
 push ix
 LD hl,x*256+y
 LD (curRow),hl
 LD H,0
 LD L, A
 B_CALL _DispHL
 B_CALL _getKey
 pop ix
 pop af
 pop bc
 pop de
 pop hl
#endmacro


;Context_Sensitive_Get_Key is used to determine what to do
;when a certain key is pressed.  Like a typical RTS
;game, one key can perform different functions
;depending on the context.  For example, 2nd can be either
;a hotkey, a firing direction, or even a mode toggle.


Context_Sensitive_Get_Key:
	
	ld b, 0

Keyloop2:

	inc b
	ld a,$FF
	out (1),a
	ld a,$80
	out (1),a
	nop
	nop
	in a,(1)
	inc a
	jr nz,_
	ld a, b
	cp 2
 	jr nz, Keyloop2
	RES Wait_Between_Presses, (IY + flag)
	
	ret
_


	ld a,$FF
	out (1),a
	ld a,$BF
	out (1),a
	nop
	nop
	in a, (1)
	ld (Current_Key_Group), a
	BIT Cursor_Mode, (IY + flag)
	jr nz, _
	bit 0, a
	call z, Context_Graph_Key_Pressed
	ld a, (Current_Key_Group)
	bit 1, a
	call z, Context_Trace_Key_Pressed
	ld a, (Current_Key_Group)
	bit 2, a
	call z, Context_Zoom_Key_Pressed
	ld a, (Current_Key_Group)
	bit 3, a
	call z, Context_Window_Key_Pressed
	ld a, (Current_Key_Group)
	bit 4, a
	call z, Context_Y_Equals_Key_Pressed
	ld a, (Current_Key_Group)

_
	bit 5, a
	call z, Context_2nd_Key_Pressed
	ld a, (Current_Key_Group)
	bit 6,a
	call z, Context_Mode_Key_Pressed
	ld a, (Current_Key_Group)
	bit 7, a
	call z, Context_Del_Key_Pressed
	
	ld a,$FF
	out (1),a
	ld a,$DF
	out (1),a
	nop
	nop
	in a, (1)
	ld (Current_Key_Group), a
	bit 5, a
	call z, Context_X_Inverse_Key_Pressed
	ld a, (Current_Key_Group)
	bit 6, a
	call z, Context_Math_Key_Pressed
	ld a, (Current_Key_Group)
	bit 7, a
	call z, Context_Alpha_Key_Pressed



	ld a,$FF
	out (1),a
	ld a,$EF
	out (1),a
	nop
	nop
	in a, (1)
	ld (Current_Key_Group), a
	bit 5, a
	call z, Context_Sine_Key_Pressed
	ld a, (Current_Key_Group)
	bit 6, a
	call z, Context_Apps_Key_Pressed
	ld a, (Current_Key_Group)
	bit 7, a
	call z, Context_X_T_Theta_N_Key_Pressed
 
	ld a,$FF
	out (1),a
	ld a,$F7
	out (1),a
	nop
	nop
	in a, (1)
	ld (Current_Key_Group), a
	bit 5, a
	call z, Context_Cosine_Key_Pressed
	ld a, (Current_Key_Group)
	bit 6, a
	call z, Context_Program_Key_Pressed
	ld a, (Current_Key_Group)
	bit 7, a
	call z, Context_Stat_Key_Pressed

	ld a,$FF
	out (1),a

	ld a,$FE
	out (1),a
	nop
	nop

	in a, (1)

	ld (Current_Key_Group), a

	and %00001001
	cp %00000000
	jr z, _

	ld a, (Current_Key_Group)
	and %00000110
	cp %00000000
	jr z, _

	ld a, (Current_Key_Group)


	bit 0, a

	call z, Context_Down_Key_Pressed
	ld a, (Current_Key_Group)
	bit 1, a
	call z, Context_Left_Key_Pressed
	ld a, (Current_Key_Group)
	bit 2, a
	call z, Context_Right_Key_Pressed
	ld a, (Current_Key_Group)
	bit 3, a
	call z, Context_Up_Key_Pressed


	BIT Ship_Mode, (IY + flag + 2)
	jr z, _
	BIT Menu_Mode, (IY + flag)
	jr nz, _

	BIT Hot_Key_Ship_Menu_Pressed, (IY + flag + 2)
	jr nz, _
	RES Load_Ship_Sprite, (IY + flag + 1)

	ld a, (Current_Key_Group)
	cp $FF
	jr z, +_
	and %00001111
	
	ld c, a	
	

	ld a, (Player1 + Player_Current_Direction)
	
	cp c
	jr z, _
	
	ld a, c
	ld (Player1 + Player_Direction),a	

	ld (Player1 + Player_Current_Direction), a
	SET Load_Ship_Sprite, (IY + flag + 1)
_
	ld a,$FF
	out (1),a
	ld a,$FD
	out (1),a
	nop
	nop
	in a, (1)
	ld (Current_Key_Group), a
	bit 0, a
	call z, Context_Enter_Key_Pressed

	ld a, (Current_Key_Group)


	bit 6, a
	call z, Context_Clear_Key_Pressed

	ld a, (Current_Key_Group)
	and %00000010
	call z, Context_Plus_Key_Pressed


	RES Hot_Key_Ship_Menu_Pressed, (IY + flag + 2)


	ret



Context_Graph_Key_Pressed:

 ld hl, 8
 ld (delta), hl
 ret

Context_Trace_Key_Pressed:

 ld hl, 6
 ld (delta), hl
 ret
	

Context_Zoom_Key_Pressed:
	
 ld hl, 4
 ld (delta), hl
 ret


Context_Window_Key_Pressed:

 ld hl, 2
 ld (delta), hl
 ret


Context_Y_Equals_Key_Pressed:

 ld hl, 1
 ld (delta), hl
 ret


	

Context_Left_Key_Pressed:

 BIT Cursor_Mode, (IY + flag)
 jr nz, Move_Cursor_Left
 BIT Menu_Mode, (IY + Flag)
 jr nz, Move_Menu_Left 
 
 BIT Ship_Mode, (IY + Flag + 2)
 jr z, Move_Map_Left
 ld hl, Player1 + Player_Direction
 res 1, (hl)
 set 2, (hl)


Move_Map_Left:

 jp movemapleft


Move_Menu_Left:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz
 SET Wait_Between_Presses, (IY + asm_flag1)


 ld a, (TagX)
 
 or a
 ret z
 sub 16
 ld (TagX),a


 ret

Move_Cursor_Left:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 SET Wait_Between_Presses, (IY + asm_flag1)
 call Move_Select_Cursor_Left
 ret










Context_Right_Key_Pressed:

 BIT Cursor_Mode, (IY + flag)
 jr nz, Move_Cursor_Right
 BIT Menu_Mode, (IY + Flag)
 jr nz, Move_Menu_Right

 BIT Ship_Mode, (IY + Flag + 2)
 jr z, Move_Map_Right
 ld hl, Player1 + Player_Direction
 res 2, (hl)
 set 1, (hl)


Move_Map_Right:

 jp movemapright

Move_Menu_Right:


 BIT Wait_Between_Presses, (IY + flag)
 ret nz
 SET Wait_Between_Presses, (IY + asm_flag1)
 ld hl, (Current_Menu)
 ld a, (hl)
 cp 1
 jr z, _

 ld a, (TagX)
 cp 32
 ret z
 add a, 16

 ld (TagX),a


 ret

_

 ld hl, (Current_Menu)
 inc hl
 ld c, (hl)
 ld a, (TagX)
 cp c
 ret z

 add a, 16
 ld (TagX), a
 ret


Move_Cursor_Right:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 SET Wait_Between_Presses, (IY + asm_flag1)
 call Move_Select_Cursor_Right

 ret














Context_Up_Key_Pressed:

 BIT Cursor_Mode, (IY + flag)
 jr nz, Move_Cursor_Up
 BIT Menu_Mode, (IY + Flag)
 jr nz, Move_Menu_Up
 BIT Ship_Mode, (IY + Flag + 2)
 jr z, Move_Map_Up
 ld hl, Player1 + Player_Direction
 res 3, (hl)
 set 0, (hl)


Move_Map_Up:

 jp movemapup

Move_Menu_Up:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz
 SET Wait_Between_Presses, (IY + asm_flag1)
 ld hl, (Current_Menu)
 ld a, (hl)
 cp 1
 ret z

 ld a, (TagY)
 cp 17
 ret z
 sub 16
 ld (TagY),a
 
 ret

Up_Key_In_Game:


Move_Cursor_Up:
 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 SET Wait_Between_Presses, (IY + asm_flag1)

 call Move_Select_Cursor_Up

 ret











Context_Down_Key_Pressed:

 BIT Cursor_Mode, (IY + flag)
 jr nz, Move_Cursor_Down
 BIT Menu_Mode, (IY + Flag)
 jr nz, Move_Menu_Down
 BIT Ship_Mode, (IY + Flag + 2)
 jr z, Move_Map_Down

 ld hl, Player1 + Player_Direction
 res 0, (hl)
 set 3, (hl)


Move_Map_Down:

 jp movemapdown


Move_Menu_Down:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz
 SET Wait_Between_Presses, (IY + asm_flag1)

 ld hl, (Current_Menu)
 ld a, (hl)
 cp 1
 ret z
 ld a, (TagY)
 cp 49
 ret z
 add a, 16
 ld (TagY),a

 ret

Move_Cursor_Down:
 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 SET Wait_Between_Presses, (IY + asm_flag1)
 call Move_Select_Cursor_Down
 ret

















Down_Key_In_Game:







Context_2nd_Key_Pressed:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 BIT Menu_Mode, (IY + flag)
 jr z, _

 SET Wait_Between_Presses, (IY + flag)
 ld hl, 0 * 256 + 17
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu
_

 RES Cursor_Mode, (IY + flag)
 RES Object_Ready_To_Add, (IY + flag)
 RES Building_Over_Solid_Object, (IY + flag + 2)
 ld de, (DeltaPreserve)
 ld (delta), de
 SET Menu_Mode, (IY + flag)
 SET Menu_Cursor_In_Center, (IY + flag)
 SET Wait_Between_Presses, (IY + flag)
 ld hl, Build_Basic_Buildings
 ld (Current_Menu), hl
 ld a, StructuresMenu
 ld (MenuIdentity), a


 ret

Second_Key_In_Game:



Context_Mode_Key_Pressed:

 BIT Menu_Mode, (IY + flag)
 jr z, _
 ld hl, 16 * 256 + 17
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu
_


 BIT Wait_Between_Presses, (IY + flag)
 ret nz
 SET Wait_Between_Presses, (IY + flag)
 call Rotate_Cursor_Mode
 ret

Context_Del_Key_Pressed:

 BIT Menu_Mode, (IY + flag)
 ret z
 ld hl, 32 * 256 + 17
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu
_



Context_X_Inverse_Key_Pressed:

Context_Math_Key_Pressed:

 BIT Menu_Mode, (IY + flag)
 ret z
 ld hl, 0 * 256 + 49
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu



Context_Alpha_Key_Pressed:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 BIT Menu_Mode, (IY + flag)
 jr z, _
 SET Wait_Between_Presses, (IY + flag)
 ld hl, 0 * 256 + 33
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu
_

 RES Cursor_Mode, (IY + flag)
 RES Object_Ready_To_Add, (IY + flag)
 ld de, (DeltaPreserve)
 ld (delta), de
 SET Menu_Mode, (IY + flag)
 SET Menu_Cursor_In_Center, (IY + flag)
 SET Wait_Between_Presses, (IY + flag)
 ld hl, Exchange_Ship
 ld (Current_Menu), hl
 ld a, ShipMenu
 ld (MenuIdentity), a

 ret



Reset_Ship_Mode:

 RES Ship_Mode, (IY + flag + 2)
 ld de, (DeltaPreserve)
 ld (Delta), de
 SET Wait_Between_Presses, (IY + flag)
 ret

Context_Sine_Key_Pressed:
	
Context_Apps_Key_Pressed:

 BIT Menu_Mode, (IY + flag)
 ret z
 ld hl, 16 * 256 + 49
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu







Context_X_T_Theta_N_Key_Pressed:

 BIT Menu_Mode, (IY + flag)
 jr z, _
 ld hl, 16 * 256 + 33
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu
_


 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 
 BIT Ship_Mode, (IY + flag + 2)
 jr nz, Reset_Ship_Mode

 BIT Cursor_Mode, (IY + flag)
 call nz, Rotate_Cursor_Mode
 SET Ship_Mode, (IY + flag + 2)
 ld de, (delta)
 ld (DeltaPreserve), de

 SET Wait_Between_Presses, (IY + flag)
 ld hl, (Player1 + Player_Map_X)
 or a
 ld de, 47
 sbc hl, de
 ld bc, Ship_Frame_Data
 ld a, (bc)
 ld d, 0
 ld e, a
 add hl, de
 ld (Map_X), hl
 ld hl, (Player1 + Player_Map_Y)
 
 or a
 ld de, 31
 sbc hl, de
 inc bc
 ld a, (bc)
 ld d, 0
 ld e, a
 add hl, de



 ld (Map_Y), hl
 

 ret

Context_Cosine_Key_Pressed:

Context_Program_Key_Pressed:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz
 
 BIT Menu_Mode, (IY + flag)
 ret z
 ld hl, 32 * 256 + 49
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu



Context_Stat_Key_Pressed:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz
 BIT Menu_Mode, (IY + flag)
 ret z
 ld hl, 32 * 256 + 33
 ld (TagY), hl
 jp Context_Enter_Key_Pressed_Menu
_


Context_Enter_Key_Pressed:


 ;BIT Object_Ready_To_Add, (IY + flag)
 ;jr z, Context_Enter_Key_Pressed_Menu

 BIT Cursor_Mode, (IY + flag)
 jr z, Context_Enter_Key_Pressed_Menu
 BIT Object_Ready_To_Add, (IY + flag)
 jr nz, Start_Construct_Building

 jr Building_Chosen_By_Enter_Key



 

Context_Enter_Key_Pressed_Menu:

 BIT MenuMode, (IY + Flag)
 JR NZ, Exit_Menu_And_Do_Stuff


 ret

Building_Chosen_By_Enter_Key:

 RES Cursor_Mode, (IY + flag)
 RES Object_Ready_To_Add, (IY + flag)
 ld de, (DeltaPreserve)
 ld (delta), de
 SET Menu_Mode, (IY + flag)
 SET Menu_Cursor_In_Center, (IY + flag)
 SET Wait_Between_Presses, (IY + flag)
 
 ld hl, (Structure_Under_Cursor) ;We need to get the correct
				;building so that we can find
				;the correct building ID.
 inc hl
 inc hl

 ld a, (hl)
 and %11110000
 
 ld d, 0
 
 rla \ rl d
 ld e, a

 ;We access menu types via building data in Unit Attributes.

 ld hl, Operation_Center
 add hl, de


 ld de, 27	;Menu is 27 away bytes away from the beginning
		;of the building data
 
 ld a, BuildingMenu
 ld (MenuIdentity), a
 add hl, de
 
 ld e, (hl)
 inc hl
 ld d, (hl)
 
 ex de, hl
 ld (Current_Menu), hl
 call Draw_Menu
 ret

Start_Construct_Building:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz
 BIT Building_Over_Solid_Object, (IY + flag + 2)
 ret nz

 call Construct_Building
 
 

 ld de, (DeltaPreserve)
 ld (delta), de
 ret


Exit_Menu_And_Do_Stuff:

 BIT Wait_Between_Presses, (IY + flag)
 jr z, _

	ld a,$FF
	out (1),a
	ld a,$FD
	out (1),a
	nop
	nop
	in a, (1)
	BIT 0, a
	ret z

_
 SET Wait_Between_Presses, (IY + asm_flag1)
 RES MenuMode, (IY + Flag)
 SET Menu_Cursor_In_Center, (IY + Flag) 


_
 call Get_X_Y_Icon_Menus

 add a, a
 cp 17		;For some unknown reason, sometimes register E becomes a
		;random value, albeit the same random value.  If that
		;is the case, redo Get_X_Y_Icon_Menus until the
		;value for e is correct.  This "bug" fix works perfectly,
		;so I see no need to remove it and
		;try to figure out the bug.
 jr nc, -_

 
 ld c, a
 
 jp Do_Menu_Operation



 ret




Context_Clear_Key_Pressed:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 BIT Menu_Mode, (IY + flag)
 jr nz, _
 BIT Cursor_Mode, (IY + flag)
 jr nz, _
 
 
 ld a, (FlagPreserve1)
 ld (IY + trigflags), a

 ld hl,$9D95
 ld de, 21000
 bcall(_DelMem)
 im 1
 bjump(_JForceCmdNoChar)

 

_

 ld de, (DeltaPreserve)
 ld (delta), de

 RES Menu_Mode, (IY + flag)
 RES Cursor_Mode, (IY + flag)
 SET Wait_Between_Presses, (IY + flag)



 ret

Context_Plus_Key_Pressed:

 BIT Wait_Between_Presses, (IY + flag)
 ret nz

 SET Wait_Between_Presses, (IY + flag)

 BIT Display_Belthium_Earned, (IY + flag + 2)
 jr nz, _

 SET Display_Belthium_Earned, (IY + flag + 2) 
 ret
_

 RES Display_Belthium_Earned, (IY + flag + 2) 
 ret

